<!--
Copyright © 2017, Che-Wei Hsu <cwxhsu@gmail.com>
This file is part of the MintCM.
Some rights reserved. See README.
-->

<html>

<head>

<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script type="text/javascript" src="jquery_main.js"></script>
<script type="text/javascript" src="json3.js"></script>
<script type="text/javascript" src="mcm_jslib_api.js"></script>
<script type="text/javascript" src="mcm_jslib_data_info_auto.js"></script>
<script type="text/javascript" src="web_app_0304_script.js"></script>
<link rel="stylesheet" href="web_app_0304_style.css">

<script type="text/javascript">
var mcm_dv = null;

function body_init()
{
    var self_jslib, req_cmd, rep_ret, data_array, i, tmp_html;


    // 指定要讀出的資料.
    req_cmd = "&get.device.vap.*" +
              "&get.device.vap.*.extra";

    // 送出讀出資料請求.
    self_jslib = new mcm_jslib_lib_t();
    self_jslib.socket_path = "@mintcm";
    self_jslib.session_permission = mcm_session_permission.ro;
    self_jslib.session_stack_size = 0;
    self_jslib.request_command = req_cmd;
    self_jslib.data_format = mcm_data_format.all_default;
    self_jslib.other_query = "";
    rep_ret = mcm_jslib_obtain_config(self_jslib);
    if(rep_ret.rep_code < mcm_return_code.pass)
    {
        alert("call mcm_jslib_obtain_config() fail" +
              "[" + rep_ret.rep_code + "]");
        mcm_jslib_run_script(rep_ret.rep_data);
        return;
    }
    // 處理資料格式.
    mcm_dv = JSON.parse(rep_ret.rep_data);

    for(i = 0; i < mcm_dv.device.vap.length; i++)
    {
        // 顯示 device.vap.{i}
        data_array = new Array(5);
        data_array[0] = i + 1;
        data_array[1] = mcm_dv.device.vap[i].ekey;
        data_array[2] = jsapi_make_text("ssid_text_" + i, 32,
            mcm_jslib_convert_html_str(mcm_dv.device.vap[i].ssid));
        data_array[3] = jsapi_make_text("channel_text_" + i, 8, mcm_dv.device.vap[i].channel);
        data_array[4] = jsapi_make_button("set", "config_vap_set_01(" + i + ")");
        tmp_html = jsapi_make_trtd("css_td1", data_array);
        $("#vap_list").find("tbody").append(tmp_html);

        // 顯示 device.vap.{i}.extra
        data_array = new Array(4);
        data_array[0] = i + 1;
        data_array[1] = jsapi_make_text("hidden_text_" + i, 4, mcm_dv.device.vap[i].extra.hidden);
        data_array[2] = jsapi_make_text("tx_power_text_" + i, 8,
            mcm_dv.device.vap[i].extra.tx_power);
        data_array[3] = jsapi_make_button("set", "config_vap_set_02(" + i + ")");
        tmp_html = jsapi_make_trtd("css_td1", data_array);
        $("#extra_list").find("tbody").append(tmp_html);
    }
}

// 設定 device.vap.*
function config_vap_set_01(
    vap_index)
{
    var self_jslib, req_cmd = "", rep_ret, tmp_path;


    // 填充 device.vap.{vap_key} 路徑.
    tmp_path = "device.vap.#" + mcm_dv.device.vap[vap_index].ekey;

    // 設定 device.vap.{vap_key}
    req_cmd += "&set." + tmp_path + ".ssid=" +
               mcm_jslib_convert_submit_str($("#ssid_text_" + vap_index).val());
    req_cmd += "&set." + tmp_path + ".channel=" +
               $("#channel_text_" + vap_index).val();

    // 送出修改資料請求.
    self_jslib = new mcm_jslib_lib_t();
    self_jslib.socket_path = "@mintcm";
    self_jslib.session_permission = mcm_session_permission.rw;
    self_jslib.session_stack_size = 0;
    self_jslib.request_command = req_cmd;
    self_jslib.after_complete = mcm_after_complete.reload;
    self_jslib.other_query = "";
    rep_ret = mcm_jslib_submit_config(self_jslib);
    if(rep_ret.rep_code < mcm_return_code.pass)
    {
        alert("call mcm_jslib_submit_config() fail" +
              "[" + rep_ret.rep_code + "]");
        mcm_jslib_run_script(rep_ret.rep_data);
        return;
    }
    mcm_jslib_run_script(rep_ret.rep_data);
}

// 設定 device.vap.*.extra
function config_vap_set_02(
    vap_index)
{
    var self_jslib, req_cmd = "", rep_ret, tmp_path;


    // 填充 device.vap.{vap_key}.extra 路徑.
    tmp_path = "device.vap.#" + mcm_dv.device.vap[vap_index].ekey + ".extra";

    // 設定 device.vap.{vap_key}.extra
    req_cmd += "&set." + tmp_path + ".hidden=" +
               $("#hidden_text_" + vap_index).val();
    req_cmd += "&set." + tmp_path + ".tx_power=" +
               $("#tx_power_text_" + vap_index).val();

    // 送出修改資料請求.
    self_jslib = new mcm_jslib_lib_t();
    self_jslib.socket_path = "@mintcm";
    self_jslib.session_permission = mcm_session_permission.rw;
    self_jslib.session_stack_size = 0;
    self_jslib.request_command = req_cmd;
    self_jslib.after_complete = mcm_after_complete.reload;
    self_jslib.other_query = "";
    rep_ret = mcm_jslib_submit_config(self_jslib);
    if(rep_ret.rep_code < mcm_return_code.pass)
    {
        alert("call mcm_jslib_submit_config() fail" +
              "[" + rep_ret.rep_code + "]");
        mcm_jslib_run_script(rep_ret.rep_data);
        return;
    }
    mcm_jslib_run_script(rep_ret.rep_data);
}

// 設定所有的 device.vap.* 和 device.vap.*.extra
function config_set_03()
{
    var self_jslib, req_cmd = "", rep_ret, i, tmp_path;


    // 處理每一筆 device.vap.*
    for(i = 0; i < mcm_dv.device.vap.length; i++)
    {
        // 填充 device.vap.{i} 路徑.
        tmp_path = "device.vap.#" + mcm_dv.device.vap[i].ekey;

        // 設定 device.vap.{i}
        req_cmd += "&set." + tmp_path + ".ssid=" +
                   mcm_jslib_convert_submit_str($("#ssid_text_" + i).val());
        req_cmd += "&set." + tmp_path + ".channel=" +
                   $("#channel_text_" + i).val();

        // 填充 device.vap.{i}.extra 路徑.
        tmp_path = "device.vap.#" + mcm_dv.device.vap[i].ekey + ".extra";

        // 設定 device.vap.{i}.extra
        req_cmd += "&set." + tmp_path + ".hidden=" +
                   $("#hidden_text_" + i).val();
        req_cmd += "&set." + tmp_path + ".tx_power=" +
                   $("#tx_power_text_" + i).val();
    }

    // 送出修改資料請求.
    self_jslib = new mcm_jslib_lib_t();
    self_jslib.socket_path = "@mintcm";
    self_jslib.session_permission = mcm_session_permission.rw;
    self_jslib.session_stack_size = 0;
    self_jslib.request_command = req_cmd;
    self_jslib.after_complete = mcm_after_complete.reload;
    self_jslib.other_query = "";
    rep_ret = mcm_jslib_submit_config(self_jslib);
    if(rep_ret.rep_code < mcm_return_code.pass)
    {
        alert("call mcm_jslib_submit_config() fail" +
              "[" + rep_ret.rep_code + "]");
        mcm_jslib_run_script(rep_ret.rep_data);
        return;
    }
    mcm_jslib_run_script(rep_ret.rep_data);
}
</script>

</head>

<body onload="body_init()">

  <div>[set][device.vap.*]</div>

  <table id="vap_list" class="css_table1">
    <thead>
      <tr>
        <td class="css_td1" colspan="5">device.vap.*</td>
      </tr>
      <tr>
        <td class="css_td1">no</td>
        <td class="css_td1">ekey</td>
        <td class="css_td1">ssid</td>
        <td class="css_td1">channel</td>
        <td class="css_td1">&nbsp;</td>
      </tr>
    </thead>
    <tbody>
    </tbody>
  </table>

  <table id="extra_list" class="css_table1">
    <thead>
      <tr>
        <td class="css_td1" colspan="4">device.vap.*.extra</td>
      </tr>
      <tr>
        <td class="css_td1">no</td>
        <td class="css_td1">hidden</td>
        <td class="css_td1">tx_power</td>
        <td class="css_td1">&nbsp;</td>
      </tr>
    </thead>
    <tbody>
    </tbody>
  </table>

  <button type="button" onclick="config_set_03()">set-all</button>

</body>

</html>
